package cn.yhbk.web.modules.manager.Controller;

import cn.yhbk.web.common.Util.MyUtil;
import cn.yhbk.web.common.api.CommonResult;
import cn.yhbk.web.modules.manager.Pojo.DTO.Activity.ActivityDto;
import cn.yhbk.web.modules.manager.Pojo.DTO.Activity.ActivityResp;
import cn.yhbk.web.modules.manager.Pojo.Entity.ActivityGpa;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbStudent;
import cn.yhbk.web.modules.manager.Pojo.VO.Activity.*;
import cn.yhbk.web.modules.manager.Service.ActivityService;
import cn.yhbk.web.modules.manager.Service.ITbStudentService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "活动接口")
@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @Autowired
    private ITbStudentService iTbStudentService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation("获取所有活动")
    @GetMapping("/getActivityList")
    public CommonResult<ActivityResp> getActivityList(){

        return CommonResult.success(activityService.getActivityList());
    }

    @ApiOperation("学生提交反馈活动【没有的活动】")
    @PostMapping("/student/stuSubmit")
    public CommonResult<String> stuSubmit(@RequestBody ActivityVo activityVo){
        Long adminStuId = MyUtil.getCurrentAdminId();
        TbStudent student = iTbStudentService.getStudentByAdminId(adminStuId);
        Boolean flag = activityService.submit(activityVo, student.getId());
        if (flag){
            return CommonResult.success("提交成功");
        }
        else{
            return CommonResult.success("提交失败");
        }
    }

    @ApiOperation("管理员审核活动")
    @PutMapping("/manage/examine/{activityExamineId}/{examineStatus}")
    public CommonResult<String> examine(@PathVariable("activityExamineId") Long activityExamineId,
                                        @PathVariable("examineStatus") Integer examineStatus){
        Long adminStuId = MyUtil.getCurrentAdminId();
        Boolean flag = activityService.examine(adminStuId, activityExamineId, examineStatus);
        if (flag){
            return CommonResult.success("操作成功");
        }
        else{
            return CommonResult.success("操作失败");
        }
    }

    @ApiOperation("管理员修改活动")
    @PutMapping("/manage/update")
    public CommonResult<String> adminUpdate(@RequestBody ActivityUpdate activityUpdate){
        Boolean flag = activityService.adminUpdate(activityUpdate);
        if (flag){
            return CommonResult.success("操作成功");
        }
        else{
            return CommonResult.success("操作失败");
        }
    }

    @ApiOperation("管理员删除活动")
    @DeleteMapping("/manage/delete/{activityNo}")
    public CommonResult<String> delete(@PathVariable("activityNo") Long activityNo){
        activityService.removeById(activityNo);
        stringRedisTemplate.delete("activityList");

        return CommonResult.success("操作成功");
    }

    @ApiOperation("管理员新增活动")
    @PostMapping("/manage/insert")
    public CommonResult insert(@RequestBody ActivityVo activityVo){
        activityService.insert(activityVo);

        return CommonResult.success("操作成功");
    }

    @ApiOperation("【学工办】活动分页")
    @GetMapping("/manage/page")
    public CommonResult<Page> page(@RequestParam("currPage") Long currPage,
                                   @RequestParam("pageSize") Long pageSize,
                                   @RequestParam(name = "categoryId", required = false) Long categoryId){

        return CommonResult.success(activityService.managePage(currPage, pageSize, categoryId));
    }

    @ApiOperation("【学工办】学生提交的审核列表")
    @GetMapping("/manage/examinePage")
    public CommonResult<Page> examinePage(@RequestParam("currPage") Long currPage,
                                   @RequestParam("pageSize") Long pageSize,
                                   @RequestParam(name = "examineStatus", required = false) Integer examineStatus){

        return CommonResult.success(activityService.examinePage(currPage, pageSize, examineStatus));
    }
    
    @ApiOperation("查询活动详情")
    @GetMapping("/manage/detail")
    private CommonResult<List<ActivityDetailResp>> getActivityDetail(@RequestParam("activityId") Long activityId){

        return CommonResult.success(activityService.getActivityDetail(activityId));
    }

    @ApiOperation("修改活动详情")
    @PutMapping("/manage/detail")
    private CommonResult updateDetail(@RequestBody ActivityDetailUpdate activityDetailUpdate){
        activityService.updateDetail(activityDetailUpdate);
        return CommonResult.success("操作成功");
    }

}
